import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { System } from '@/schemas/system'; // 确保路径正确

// 定义 status state 的初始值
const initialState: System = {
  isLoading: false,
  routes: [], // 假设初始没有路由
  panel: { right: false, bottom: false, left: false },
  theme: 'light',
};

const publicSlice = createSlice({
  name: 'pub',
  initialState,
  reducers: {
    setTheme: (state, action: PayloadAction<'dark' | 'light'>) => {
      state.theme = action.payload
    },
    setSelected: (state, action: PayloadAction<System['selected']>) => {
      state.selected = action.payload;
    },
    toggleTheme: (state, action: PayloadAction<HTMLElement>) => {
      const currentIsDark = state.theme === 'dark'
      state.theme = currentIsDark ? 'light' : 'dark';
      if (currentIsDark) {
        action.payload.removeAttribute('theme-mode');
      } else {
        action.payload.setAttribute('theme-mode', 'dark');
      }

    },
    togglePanel: (state, action: PayloadAction<'left' | 'right' | 'bottom'>) => {
      state.panel[action.payload] = !state.panel[action.payload];
    },
    // 定义设置加载状态的 action
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.isLoading = action.payload;
    },
    // 定义更新路由的 action
    setRoutes: (state, action: PayloadAction<string[]>) => {
      state.routes = action.payload;
    },
    // 你可以根据需要添加更多 reducers
    pushRoute: (state, action: PayloadAction<string>) => {
      if (state.routes[state.routes.length - 1] !== action.payload) {
        state.routes.push(action.payload);
      }
    },
    popRoute: (state) => {
      state.routes.pop();
    }
  },
});

// 导出 actions
export const PUB = publicSlice.actions;

// 导出 reducer
export default publicSlice.reducer;